package com.example.distinguish_server.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.distinguish_server.Utils.Html.ResponseUtil;
import com.example.distinguish_server.Utils.ThreadCache.CacheSingleton;
import com.example.distinguish_server.entity.Vo.PlateDeteVo;
import com.example.distinguish_server.entity.prop.JsonRootBean;
import com.example.distinguish_server.entity.prop.TokenResponse;
import com.example.distinguish_server.entity.prop.Words_result;
import com.example.distinguish_server.result.RLIST;
import com.example.distinguish_server.service.PlateNumberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-09-20
 */
@RestController
@RequestMapping("/plate-number")
public class PlateNumberController {

    @Autowired
    private PlateNumberService plateNumberService;

//    车牌识别

    @ApiOperation(value = "车牌识别")
    @PostMapping("plateDetection")
    public RLIST plateDetection (@RequestParam("file") MultipartFile multipartFile, PlateDeteVo plateDeteVo){

        String originalFilename = multipartFile.getOriginalFilename();

        String format = originalFilename.substring(originalFilename.length() - 4);
        if(!(format.equalsIgnoreCase(".jpg") || format.equalsIgnoreCase("jpeg"))){
            return RLIST.error().message("格式异常 需要jpg或jpeg");
        }
        String filePath = "D:\\plateDetection\\";
        File file = new File(filePath+originalFilename);
        if(!file.getParentFile().exists()){   //这里就判断了文件夹是否存在
            file.getParentFile().mkdirs();	//如果不存在就创建文件夹
        }
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        CacheSingleton instance = CacheSingleton.getInstance();
        String token = instance.getValue("token");
        if(token == null || token.equals("")){
            TokenResponse budaiToken = ResponseUtil.getBudaiToken();
            token = budaiToken.getAccess_token();
            instance.setValue("token",token,budaiToken.getExpires_in());

        }
        String budaiData = ResponseUtil.getBudaiData(originalFilename, token, plateDeteVo.getMultiDetect(), plateDeteVo.getMultiScal());
        JSONObject jsonObject = JSONObject.parseObject(budaiData);
        JsonRootBean jsonRootBean = JSON.toJavaObject(jsonObject, JsonRootBean.class);
        //是否存在
        List<String> listStr = new ArrayList<>();
        for (Words_result words_result : jsonRootBean.getWords_result()) {
            String number = words_result.getNumber();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("number",number);
            int count = plateNumberService.count(wrapper);
            if(count>0){
                listStr.add(number);
            }
        }



        if(listStr == null || listStr.size() == 0){
            return RLIST.error().message("不存在");
        } else {
            return RLIST.ok().data("data",listStr);
        }


    }
}

